//
//  JSCollectionViewLayout.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/29.
//  https://www.jianshu.com/p/2504e6e5504c
//  UICollectionViewFlowLayout 实现横向布局分页

import UIKit

/// 实现横向布局分页
class JSCollectionViewLayout: UICollectionViewFlowLayout {
    /// 行数
    var numRow:Int = 0
    /// 列数
    var numCol:Int = 0
    var contentInsets: UIEdgeInsets =  UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)   //内容外边距
    //所有cell的布局属性
    var layoutAttributes: [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
    
    /// 水平距离,左右间距
    var hor_spacing:CGFloat = 0
    /// 垂直距离,上下间距
    var ver_spacing:CGFloat = 0
    override init() {
        super.init();
        self.itemSize = CGSize(width: 50, height: 50);  //这里可以根据自己的需求设置大小
        self.scrollDirection = .horizontal
        self.numRow = 7;
        self.numCol = 4;
        self.hor_spacing = 10
        self.ver_spacing = 10
        self.contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //计算布局
    override func prepare() {
        self.itemSize.width = (self.collectionView!.frame.width-self.contentInsets.left-contentInsets.right-CGFloat(self.numRow-1)*hor_spacing)/CGFloat(self.numRow)
        self.itemSize.height = (self.collectionView!.frame.height-self.contentInsets.left-contentInsets.right-CGFloat(self.numCol-1)*ver_spacing)/CGFloat(self.numCol)
        
//        //计算水平距离
//        hor_spacing = (self.collectionView!.frame.width - CGFloat(self.numRow) * self.itemSize.width - self.contentInsets.left - self.contentInsets.right) / CGFloat(self.numRow - 1)
//
//        //计算垂直距离
//        ver_spacing = (self.collectionView!.frame.height - CGFloat(self.numCol) * self.itemSize.height - self.contentInsets.top - self.contentInsets.bottom) / CGFloat(self.numCol - 1)
        
        let numsection = self.collectionView!.numberOfSections;
        let itemNum: Int = self.collectionView!.numberOfItems(inSection: 0)
        layoutAttributes.removeAll()
        for i in 0..<numsection{
            for j in 0..<itemNum{
                let layout = self.layoutAttributesForItem(at: IndexPath(item: j, section: i))!;
                self.layoutAttributes.append(layout);
            }
        }
        
    }
    
    /**
     返回true只要显示的边界发生改变就重新布局:(默认是false)
     内部会重新调用prepareLayout和调用
     layoutAttributesForElementsInRect方法获得部分cell的布局属性
     */
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    /*
     根据indexPath去对应的UICollectionViewLayoutAttributes  这个是取值的，要重写，在移动删除的时候系统会调用改方法重新去UICollectionViewLayoutAttributes然后布局
     */
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
//        assert((self.collectionView!.frame.width - self.contentInsets.left - self.contentInsets.right) >= (self.itemSize.width*CGFloat(self.numRow)), "布局宽度不能超过父试图宽度")
//
//        assert((self.collectionView!.frame.height - self.contentInsets.top - self.contentInsets.bottom) >= self.itemSize.height*CGFloat(self.numCol), "布局高度不能超过父视图高度")
        
        let layoutAttribute = super.layoutAttributesForItem(at: indexPath)
        
       
        
        //计算x的位置
        var frame_x = CGFloat(indexPath.section) * self.collectionView!.frame.width + CGFloat(indexPath.row%self.numRow) * self.itemSize.width + self.contentInsets.left;
        frame_x += (hor_spacing*(CGFloat(indexPath.row%self.numRow)))
        
        
        
        //计算y的位置
        var frame_y = CGFloat((indexPath.row/self.numRow)) * self.itemSize.height + self.contentInsets.top;
        
        frame_y += (ver_spacing*CGFloat(indexPath.row/self.numRow))
        
        layoutAttribute?.frame = CGRect(x:frame_x, y:frame_y, width: self.itemSize.width, height: self.itemSize.height)
        
        return layoutAttribute
        
    }
    
    override open var collectionViewContentSize: CGSize {
        
        return CGSize(width: self.collectionView!.frame.width * CGFloat(self.collectionView!.numberOfSections), height: self.collectionView!.frame.height)
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        return self.layoutAttributes
    }
    
    
}


/**
 使用：
 let collectionView = UICollectionView(frame: M_RECT(0, y: NEWHEIGHT(0), w: kScreenW, h: kScreenH - 64 - NEWHEIGHT(0)), collectionViewLayout: SYCollectionViewLayout());
 collectionView.delegate = self;
 collectionView.dataSource = self;
 
 
 /**
 返回每个分组有几个数据
 */
 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
 
 return 32;
 }
 /**
 返回有几个分组
 */
 func numberOfSections(in collectionView: UICollectionView) -> Int
 {
 
 return 2;
 }
 */
